package org.nanotek.tika.tests;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;

import org.apache.tika.detect.DefaultDetector;
import org.apache.tika.detect.TypeDetector;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.CompositeParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.PasswordProvider;
import org.apache.tika.sax.BodyContentHandler;
import org.apache.tools.ant.DirectoryScanner;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.nanotek.util.HibernateUtil;
import org.xml.sax.helpers.DefaultHandler;

public class Dirscan {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		DirectoryScanner scanner = new DirectoryScanner();  
		File [] roots = File.listRoots();
		String [] rootStrArray = new String[roots.length]; 

		scanner.setIncludes(new String[]{"/**/*.java"});  
		scanner.setBasedir("\\");
		
		scanner.setCaseSensitive(false);  
		scanner.scan();  
		String[] files = scanner.getIncludedFiles(); 
		for (String fileStr : files) 
		{ 
			String file1 = fileStr.replace("\\", "/");
			try {
				ParseContext context = new ParseContext();
				DefaultDetector detector = new DefaultDetector();
				AutoDetectParser parser = new AutoDetectParser(detector);
				context.set(Parser.class, parser);
				context.set(PasswordProvider.class, new PasswordProvider() {
					public String getPassword(Metadata metadata) {
						String password = "";
						return password ;
					}
				});
				String fileLoc = scanner.getBasedir() + "/" + file1;
				System.out.println(fileLoc);
				File file = new File(fileLoc);
				InputStream is = new FileInputStream(file);
				InputStream input = TikaInputStream.get(is); 
				System.out.println(input.available());
			} catch ( Exception e) {
				e.printStackTrace();
			}
		}
//		session.close();
	}

	private static void handleStream(InputStream input, Metadata md) throws Exception {

		StringBuilder metadataBuffer = new StringBuilder();
		Metadata met = new Metadata();

		ParseContext context = new ParseContext();
		DefaultDetector detector = new DefaultDetector();
		AutoDetectParser parser = new AutoDetectParser(detector);
		context.set(Parser.class, parser);
		context.set(PasswordProvider.class, new PasswordProvider() {
			public String getPassword(Metadata metadata) {
				String password = "";
				return password ;
			}
		});

		BodyContentHandler bodyHandler =  new BodyContentHandler(System.out);
		bodyHandler.startDocument();
		parser.parse(input,  bodyHandler, met,  context);

		for(int i = 0; i <met.names().length; i++) {
			String name = met.names()[i];
			System.out.println(name + " : " + met.get(name));
		}
		System.out.println("BodyHandler " + bodyHandler.toString());
	}

}
